(function() {
	'use strict';

	angular.module('e-shop')
		.factory('AuthInterceptor',AuthInterceptor);

		AuthInterceptor.$inject=['$rootScope', '$q', '$location', '$injector','$cookieStore','AUTH_EVENTS']

		function AuthInterceptor($rootScope, $q, $location, $injector,$cookieStore,AUTH_EVENTS) {
			var Auth;
			return {
				request: function(config) {
					config.headers = config.headers || {};
					if (!config.headers.Authorization&&$cookieStore.get('CFTOKEN')) {
						config.headers.Authorization = $cookieStore.get('CFTOKEN').replace(/(^\")|(\"$)/g, "");
					}
					return config;
				},
				response: function(response) {
					if(response.data)
					$rootScope.$broadcast({					
						901: AUTH_EVENTS.notAuthenticated,
						401: AUTH_EVENTS.notAuthorized
						// 410: AUTH_EVENTS.notAuthorized,
						// 411: AUTH_EVENTS.notAuthenticated,
						// 419: AUTH_EVENTS.sessionTimeout,
						// 440: AUTH_EVENTS.sessionTimeout,
						// 481: AUTH_EVENTS.notBaseadminAuth,
						// 700: AUTH_EVENTS.systemError
					}[response.data.code], response);
					return $q.resolve(response);
				},
				responseError: function(rejection) {
					return rejection;
				}
			};
		}
})();